package offer;

/**
 * @author pengfei.hpf
 * @date 2020/2/21
 * @verdion 1.0.0
 */
public class BigNumberAdd {
    public String add(String a, String b) {
        if (a == null || b == null) {
            //
            return null;
        }

        StringBuilder builder = new StringBuilder();
        if (a.length() < b.length()) {
            String tmp = a;
            a = b;
            b = tmp;
        }
        int len = b.length();
        StringBuilder b1 = new StringBuilder(a);
        b1.reverse();
        StringBuilder b2 = new StringBuilder(b);
        b2.reverse();
        int flag = 0;
        int i = 0;
        for (; i < len; i++) {
            int c1 = b1.charAt(i) - '0';
            int c2 = b2.charAt(i) - '0';
            int s = c1 + c2 + flag;
            if (s >= 10) {
                s = s - 10;
                flag = 1;
            } else {
                flag = 0;
            }
            builder.insert(0, s);
        }
        while (i < a.length()) {
            int c1 = b1.charAt(i) - '0';
            int s = c1 + flag;
            if (s >= 10) {
                s = s - 10;
                flag = 1;
            } else {
                flag = 0;
            }
            builder.insert(0, s);
            i++;
        }
        if(flag != 0){
            builder.insert(0, flag);
        }
        return builder.toString();
    }

    public static void main(String[] args){
        System.out.println((new BigNumberAdd()).add("1000000", "999"));

    }
}
